﻿using UnityEngine;
using System.Collections;
using UI;
using System.Collections.Generic;
using Chat;
using ProtoBuf.Message;

public class UINewUnionFightChatPage : Page {
    public UIScrollView scrollView;
    public UIGrid grid;
    public GameObject MyItemObj;
    public GameObject OtherItemObj;
    public UIInput input;
    public UILabel NumbleLab;
    private ChatInfo lastchatinfo;
    private List<GameObject> items = new List<GameObject>();
    private string context = "";
    private int maxNum;

    bool reLifePanelOpen = false;
    #region region overried

    void Start()
    {
        grid.onReposition = OnGridReposition;
    }
    
    protected override void DoOpen()
    {
        maxNum = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MaxChatNumber;
    }
    public override void Close()
    {
        if(!reLifePanelOpen)
            GetComponent<TweenPosition>().PlayForward();
        else
            GetComponent<TweenPosition>().PlayReverse();
        //base.Close();
    }
    #endregion

    void OnGridReposition()
    {
        if(scrollView.verticalScrollBar!=null&&scrollView.verticalScrollBar.value<0.9)
        {
            scrollView.ResetPosition();
        }
    }

    void Update()
    {
        NumbleLab.text = string.Concat(input.value.Length, "/40");
        if (ChatManager.Instance.newFightChatInfo==true)
        {
            RefleshChatList();
            ChatManager.Instance.newFightChatInfo = false;
        }
    }

    void RefleshChatList()
    {
        int index = lastchatinfo == null ? -1 : ChatManager.Instance.fightChatInfoQue.IndexOf(lastchatinfo);
        int newCount = ChatManager.Instance.fightChatInfoQue.Count - index - 1;
        int needRemove = newCount + items.Count - maxNum;
        for(int i=0;i<needRemove;i++)//回收超出的obj
        {        
            RecycleItem(items[0]);
            items.RemoveAt(0);
        }//创建新的obj
        for(int i=index+1;i<ChatManager.Instance.fightChatInfoQue.Count;i++)
        {
            lastchatinfo = ChatManager.Instance.fightChatInfoQue[i];
            if (lastchatinfo.playerId == StaticData.playerData.playerId)
            {
                items.Add(CreateMyItem(lastchatinfo));
            }
            else
                items.Add(CreateOtherItem(lastchatinfo));
        }
        if (ChatManager.Instance.fightChatInfoQue.Count == 0)
            lastchatinfo = null;
        else
            lastchatinfo = ChatManager.Instance.fightChatInfoQue[ChatManager.Instance.fightChatInfoQue.Count - 1];
        ResetPosition();
    }
    void RecycleItem(GameObject obj)
    {
        GameObjectPoolManager.Instance.Unspawn(obj);
    }
    void ResetPosition()
    {
        grid.repositionNow = true;
    }
    GameObject CreateMyItem(ChatInfo info)
    {
        GameObject item = GameObjectPoolManager.Instance.Spawn(MyItemObj);
        item.transform.parent = grid.transform;
        item.transform.localPosition = Vector3.zero;
        item.transform.localEulerAngles = Vector3.zero;
        item.transform.localScale = Vector3.one;
        //item.GetComponent<MyChatItem>().InitMyChatItem()
        return item;
    }

    GameObject CreateOtherItem(ChatInfo info)
    {
        GameObject item = GameObjectPoolManager.Instance.Spawn(MyItemObj);
        item.transform.parent = grid.transform;
        item.transform.localPosition = Vector3.zero;
        item.transform.localEulerAngles = Vector3.zero;
        item.transform.localScale = Vector3.one;
        //item.GetComponent<MyChatItem>().InitMyChatItem()
        return item;
    }

    public void WorldSendBtClick(GameObject obj)
    {
        context = input.value;
        if (context.Length > 40)
        {
            BoxManager.CreatePopupTextBox("字数超过40个字");
            return;
        }
        else if (context.Length <= 0)
        {
            BoxManager.CreatePopupTextBox("输入为空,无法发送");
            return;
        }
        else
            SentMessage();
    }
    public void SentMessage()
    {
        CSChatTcp mes = new CSChatTcp();
        mes.chatType = (int)Chat.ChatType.Fight;
        mes.content = context;
        mes.playerId = StaticData.playerData.playerId;
        NetAPI.SendSocket(OpDefine.CSChatTcp, mes);
        CleanInput();
    }
    void CleanInput()
    {
        NumbleLab.text = "0/40";
        input.value = "";
        context = "";
        input.label.GetComponent<UILabel>().text = "";
    }
    public void CloseChatPage(GameObject obj)
    {
        Close();
    }
	
}
